package com.hitrust.gs.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSBalanceMsgBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSStocksOtherBean;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSStocksProcesser;
import com.hitrust.gs.dao.GSBalanceMsgDAO;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.form.GSUnBalanceDetlQryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSUnBalanceDetailQryVO;

public class GSUnBalanceDetailQueryAction extends GSBaseAction {
    private GSStocksProcesser stpro=new GSStocksProcesser();
	public String execute(GSBaseForm aForm) throws Exception {
		GSUnBalanceDetlQryForm tForm = (GSUnBalanceDetlQryForm)aForm;
		GSUnBalanceDetailQryVO tVO = new GSUnBalanceDetailQryVO();
		try{
			tVO.concentricityId = tForm.getConcentricityId();
	        String[] tDateStr = new String[2];
	        
	        tDateStr[0] = tForm.orderDeliverDateStr;
	        tDateStr[1] = tForm.orderDeliverDateEnd;
	        tDateStr = DateUtil.formateDate(tDateStr);
	        tVO.deliverGoodsDateStr = tDateStr[0];
	        tVO.deliverGoodsDateEnd = tDateStr[1];
			tVO.deliverGoodsId = tForm.getDeliverGoodsId();
			tVO.manufInfo = tForm.getManufInfo();
			tVO.providerShortName = tForm.getProviderName();
			tVO.balanceYM = tForm.getBalanceYM();
			tVO.deptId = tForm.getDeptId();
			tVO.deptCategory = tForm.getDeptCategory();
			if(!tForm.orderDeptId.equals("0000")){
			    tVO.orderDeptId=tForm.getOrderDeptId();
			}
	        aForm.getRequest().setAttribute("deptId",tVO.deptId);
	        aForm.getRequest().setAttribute("balanceYM",tVO.balanceYM);
	        aForm.getRequest().setAttribute("deptCategory",tVO.deptCategory);
	        //取得当前操作员可查询的订货单位和送货地址
	        GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
	        GSOrderDeptInfoProcessor tOrderDeptProc = new GSOrderDeptInfoProcessor();
	        HashMap tMap = tOrderDeptProc.getOrderDeptInfoOfBusOper(tOperBean.getGs_oper_org_id());
	        aForm.getRequest().setAttribute("orderdept",tMap);
	        ArrayList tOrgList = (ArrayList)tMap.get("organization");
	        //取得分页参数
	        String[] tStr = this.getPageParams(tForm,tVO,tOrgList);
	        
	        GSOrderProcessor tOrderProc = new GSOrderProcessor();
	        ArrayList tList = tOrderProc.getSignedDetail(tVO,tOrgList,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
	        String dou1="0";
	        BigDecimal d1=new BigDecimal(0);
	        BigDecimal d2=new BigDecimal(0);
	        if (tList.size()>0) {
                for (int u = 0; u < tList.size(); u++) {
                    String[] temp=(String[]) tList.get(u);
                    d2=new BigDecimal(Double.parseDouble(temp[17])*Double.parseDouble(temp[18]));
                    d1=d1.add(d2);
                }
                dou1=String.valueOf(d1.doubleValue());
            }
	        tForm.getRequest().setAttribute("ordersum",dou1);
	        ResultProcess tRp = new ResultProcess();
	        tRp.setResult(tList);
	        tForm.getRequest().setAttribute("order",tRp);
	        
	        tForm.getRequest().setAttribute("pageSize",tStr[1]);
	        tForm.getRequest().setAttribute("totalRecords",tStr[2]);
	        //显示是否包含压库应付
	        String[] str={tForm.getDeptId(),tForm.getBalanceYM(),GSBaseConstants.BALANCE_MODE2};
	        ArrayList arstocks=stpro.getStocksDetailByMonthProvid(str);
	        String dou2="0";
	        BigDecimal dl1=new BigDecimal(0);
	        BigDecimal dl2=new BigDecimal(0);
	        if (arstocks.size()>0) {
                for (int i = 0; i < arstocks.size(); i++) {
                    GSStocksOtherBean obean=(GSStocksOtherBean) arstocks.get(i);
                    dl2=new BigDecimal(obean.getGs_detail_count()*obean.getGs_detail_price());
                    dl1=dl1.add(dl2);
                }
                dou2=String.valueOf(dl1.doubleValue());
            }
	        tForm.getRequest().setAttribute("stockssum",dou2);
		}catch(Exception e){
			tForm.getRequest().setAttribute("window","true");
			throw e;
		}    
        return  "orderdeptdetail";
	}
	   private String[] getPageParams(GSBaseForm aForm,GSUnBalanceDetailQryVO aOrderVO,ArrayList aOrg) throws Exception{
	        String tStr[] = new String[3];
	        int totalRecords;
	        int pageNo;
	        int pageSize;
	        String tPage = GSBaseConstants.tPage;
	        //根据页面传入的查询参数查询订单信息
	        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
                GSBalanceMsgDAO tBalMsgDAO = new GSBalanceMsgDAO();
                ArrayList tBalList = tBalMsgDAO.queryBalanceInfo(aOrderVO.deptId,aOrderVO.balanceYM);
                if(tBalList.size() == 1){
                    GSBalanceMsgBean tBean = (GSBalanceMsgBean)tBalList.get(0);
                    aOrderVO.endBalDate = DateUtil.splitSpecChar(tBean.getGs_end_date());
                }
	            GSOrderDetailDAO tempOrderDAO = new GSOrderDetailDAO();
	            totalRecords = tempOrderDAO.querySignedDetailCount(aOrderVO,aOrg);
	            // 取得系统默认的页大小
	            pageSize = Integer.parseInt(tPage);
	            // PageNo=1
	            pageNo = 1;
	            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
	            if (pageNo>n) {
	                pageNo=n;
	            }
	            tStr[0] = "" + pageNo;
	            tStr[1] = "" + pageSize;
	            tStr[2] = "" + totalRecords;
	        }else{
	            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
	        }
	        return tStr;
	    }
}
